package com.hdyg.sixg.util;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.hdyg.sixg.R;

import java.io.Serializable;

/**
 * Alert Dialog
 *
 * @author CZB
 * @time 2018/3/7 10:25
 */
public class AlertDialogUtil implements Serializable {

    private static class AlertDialogHolder {
        /**
         * 单例对象实例
         */
        static final AlertDialogUtil INSTANCE = new AlertDialogUtil();
    }

    public static AlertDialogUtil getInstance() {
        return AlertDialogHolder.INSTANCE;
    }

    /**
     * private 的构造方法，避免外界直接使用new 来初始化对象
     */
    private AlertDialogUtil() {
    }

    /**
     * 应对单例对象被序列化时候
     *
     * @return
     */
    private Object readResolve() {
        return getInstance();
    }

    public void showDialog(Context mContext,
                           String title, String content,
                           final OnClickYesListener yesListener,
                           final OnClickNoListener noListener,
                           boolean cancelable) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.sys_window_alert_dialog, null);
        TextView dialogTitle = (TextView) view.findViewById(R.id.dialogTitle);
        TextView dialogContent = (TextView) view.findViewById(R.id.dialogContent);
        View dialogCutOfRule = (View) view.findViewById(R.id.dialogCutOfRule);
        TextView dialogCertain = (TextView) view.findViewById(R.id.dialogCertain);
        TextView dialogCancel = (TextView) view.findViewById(R.id.dialogCancel);

        dialogContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        dialogContent.setScrollbarFadingEnabled(false);

        // 实例化 AlertDialog
        final AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
        // 获取 AlertDialog 的窗体
        Window window = alertDialog.getWindow();
        // 设置背景透明
        window.setBackgroundDrawableResource(android.R.color.transparent);

        // 标题
        if (title == null || "".equals(title)) {
            dialogTitle.setVisibility(View.GONE);
            dialogContent.setTextSize(14);
        } else {
            dialogTitle.setText(title);
        }
        // 内容
        dialogContent.setText(content);
        // 设置点击系统返回可以关闭（默认）
        alertDialog.setCancelable(cancelable);
        // 显示
        alertDialog.show();
        // 加载布局，必须在 show 之后
        window.setContentView(view);

        // 确定
        dialogCertain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesListener.onClickYes();
                alertDialog.dismiss();
            }
        });

        // 取消
        dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noListener.onClickNo();
                alertDialog.dismiss();
            }
        });

    }
    public void showDialog(Context mContext,
                           int title, int content,
                           final OnClickYesListener yesListener,
                           final OnClickNoListener noListener,
                           boolean cancelable) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.sys_window_alert_dialog, null);
        TextView dialogTitle = (TextView) view.findViewById(R.id.dialogTitle);
        TextView dialogContent = (TextView) view.findViewById(R.id.dialogContent);
        View dialogCutOfRule = (View) view.findViewById(R.id.dialogCutOfRule);
        TextView dialogCertain = (TextView) view.findViewById(R.id.dialogCertain);
        TextView dialogCancel = (TextView) view.findViewById(R.id.dialogCancel);

        dialogContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        dialogContent.setScrollbarFadingEnabled(false);

        // 实例化 AlertDialog
        final AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
        // 获取 AlertDialog 的窗体
        Window window = alertDialog.getWindow();
        // 设置背景透明
        window.setBackgroundDrawableResource(android.R.color.transparent);

        // 标题
        dialogTitle.setText(title);
        // 内容
        dialogContent.setText(content);
        // 设置点击系统返回可以关闭（默认）
        alertDialog.setCancelable(cancelable);
        // 显示
        alertDialog.show();
        // 加载布局，必须在 show 之后
        window.setContentView(view);

        // 确定
        dialogCertain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesListener.onClickYes();
                alertDialog.dismiss();
            }
        });

        // 取消
        dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noListener.onClickNo();
                alertDialog.dismiss();
            }
        });

    }

    public void showDialog(Context mContext,
                           String title, String content,
                           final OnClickYesListener yesListener,
                           boolean cancelable) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.sys_window_alert_dialog, null);
        TextView dialogTitle = (TextView) view.findViewById(R.id.dialogTitle);
        TextView dialogContent = (TextView) view.findViewById(R.id.dialogContent);
        View dialogCutOfRule = (View) view.findViewById(R.id.dialogCutOfRule);
        TextView dialogCertain = (TextView) view.findViewById(R.id.dialogCertain);
        TextView dialogCancel = (TextView) view.findViewById(R.id.dialogCancel);

        dialogCutOfRule.setVisibility(View.GONE);
        dialogCancel.setVisibility(View.GONE);

        // 实例化 AlertDialog
        final AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
        // 获取 AlertDialog 的窗体
        Window window = alertDialog.getWindow();
        // 设置背景透明
        window.setBackgroundDrawableResource(android.R.color.transparent);

        // 标题
        if (title == null || "".equals(title)) {
            dialogTitle.setVisibility(View.GONE);
            dialogContent.setTextSize(14);
        } else {
            dialogTitle.setText(title);
        }
        // 内容
        dialogContent.setText(content);
        // 设置点击系统返回可以关闭（默认）
        alertDialog.setCancelable(cancelable);
        // 显示
        alertDialog.show();
        // 加载布局，必须在 show 之后
        window.setContentView(view);

        // 确定
        dialogCertain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesListener.onClickYes();
                alertDialog.dismiss();
            }
        });
    }

    /**
     * 确认
     */
    public interface OnClickYesListener {
        void onClickYes();
    }

    /**
     * 取消
     */
    public interface OnClickNoListener {
        void onClickNo();
    }
}
